package com.efounder.util;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

/**
 * 
 * @author Javor
 *
 */

public class StorageUtil {
	private SharedPreferences sharedPreferences;			
	private Editor editor;

	public StorageUtil(Context context, String xmlName){
		sharedPreferences = context.getSharedPreferences(xmlName, Activity.MODE_PRIVATE);
		editor = sharedPreferences.edit();
	}
	
	/**
	 * 保存数据到SD Card xml文件    需要调用commit
	 * @param key
	 * @param value
	 */
	public void putString(String key, String value){
		editor.putString(key, value);
	}
	
	public void putBoolean(String key, Boolean value){
		editor.putBoolean(key, value);
	}
	
	public void putInt(String key, int value){
		editor.putInt(key, value);
	}
		
	/**
	 * 提交改动
	 */
	public void commit(){
		editor.commit();
	}
	public void apply(){
		editor.apply();
	}
	/**
	 * 建议使用 getString(String key, String defValue)
	 */
	public String getString(String key){
		return sharedPreferences.getString(key, "");
	}
	
	/**
	 * 从SD Card xml文件获取数据
	 * @param key
	 * @return
	 */
	public String getString(String key, String defValue){
		return sharedPreferences.getString(key, defValue);
	}
	
	public Boolean getBoolean(String key, Boolean defValue){
		return sharedPreferences.getBoolean(key, defValue);
	}
	
	public int getInt(String key, int defValue){
		return sharedPreferences.getInt(key, defValue);
	}

	/**
	 * 删除SD Card xml文件中某个Item    需要调用commit
	 * @param key
	 */
	public void removeItem(String key) {
		editor.remove(key);
	}

	/**
	 * 清除SD Card xml文件中所有Item
	 * @param xmlName  xml文件名
	 */
	public void clearData(String xmlName) {
		editor.clear().commit();
	}
}
